package com.java.zhuiyun.api.live.controller;

import com.java.zhuiyun.api.live.entity.Live;
import com.java.zhuiyun.api.live.service.LiveService;
import com.java.zhuiyun.common.response.CommonResult;
import com.java.zhuiyun.util.TencentYunVedioUtil;
import com.java.zhuiyun.util.pageUtil.PageRequest;
import com.java.zhuiyun.util.pageUtil.PageResult;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;

/**
 * @ClassName LiveController
 * @Description TODO
 * @Author MJL
 * @Date 2023/8/14 14:01
 **/
@RestController
@RequestMapping("/live")
public class LiveController {
    @Resource
    LiveService liveService;
    /**
    * @Author MJL
    * @Description todo 查看所有直播间信息
    * @Date 2023/8/21 10:05
    * @Return  * @param null
    */
    @GetMapping("/queryLive")
    public CommonResult queryLive(Live live, PageRequest pageRequest){
        return CommonResult.ok(liveService.queryLive(live,pageRequest));
    }
    /**
    * @Author MJL
    * @Description todo 查询直播记录
    * @Date 2023/9/6 11:17
    * @Return  * @param null
    */
    /**
    * @Author MJL
    * @Description todo 将违规直播间封禁
    * @Date 2023/8/21 14:01
    * @Return  * @param null
    */
    @PostMapping("/banned")
    public CommonResult banned(Integer id) {
        return CommonResult.ok(liveService.banned(id));
    }
    /**
    * @Author MJL
    * @Description todo 将直播间解封
    * @Date 2023/8/21 14:11
    * @Return  * @param null
    */
    @PostMapping("/enable")
    public CommonResult enable(Integer id){
        return CommonResult.ok(liveService.enable(id));
    }
    /**
    * @Author MJL
    * @Description todo 添加直播间（开启直播）
    * @Date 2023/8/23 8:50
    * @Return  * @param null
    */
    @PostMapping("/saveLive")
    public CommonResult saveLive(Live live, MultipartRequest multipartRequest , MultipartFile file) throws IOException {
        return liveService.saveLive(live,multipartRequest,file);
    }
    /**
    * @Author MJL
    * @Description todo 根据教师id获取直播信息
    * @Date 2023/8/23 14:35
    * @Return  * @param null
    */
    @GetMapping("/getLiveByUserId")
    CommonResult getLiveByUserId(Integer teacherId) {
        return liveService.getLiveByUserId(teacherId);
    }
    /**
    * @Author MJL
    * @Description todo 关闭直播
    * @Date 2023/8/23 15:06
    * @Return  * @param null
    */
    @GetMapping("/closeLive")
    public CommonResult closeLive(Live live) {
        return liveService.closeLive(live);
    }
    /**
    * @Author MJL
    * @Description todo 检验当前主播是否正在直播
    * @Date 2023/8/23 15:28
    * @Return  * @param null
    */
    @GetMapping("/checkLiveByUserId")
    public CommonResult checkLiveByUserId(Integer teacherId) {
        return liveService.checkLiveByUserId(teacherId);
    }
    /**
    * @Author MJL
    * @Description 获取正在直播的直播间
    * @Date 2023/8/24 19:17
    * @Return  * @param null
    */
    @GetMapping("/listLive")
    public CommonResult listLive() {
        return liveService.listLive();
    }
    /**
    * @Author MJL
    * @Description todo 根据id获取直播间信息
    * @Date 2023/8/24 16:12
    * @Return  * @param null
    */
    @GetMapping("/getLiveById")
    public CommonResult getLiveById(Integer id) {
        return liveService.getLiveById(id);
    }
    /**
    * @Author MJL
    * @Description todo 生成推流路径地址
     *
     * pushUrl 推流地址 头部
     * 直播码  调用该方法genLiveCode() 生成
     * push_key 推流鉴权
     *
    * @Date 2023/8/23 9:28
    * @Return  * @param null
    */
    @PostMapping("/createPushUrlAndPlayUrl")
    public static HashMap<Object, Object> createPushUrlAndPlayUrl(String streamName) {
        // 过期时间 16进制的unix时间戳
        String txTime = TencentYunVedioUtil.to16Hex(TencentYunVedioUtil.addOneDay());
        // 生成推流地址
        StringBuffer sb = new StringBuffer();
        String pushUrlHead = TencentYunVedioUtil.pushUrl + streamName;
        // 生成串流密钥
        sb.append(streamName).append("?").append("txSecret=")
                .append(TencentYunVedioUtil.genSign(TencentYunVedioUtil.push_key, streamName,txTime))
                .append("&").append("txTime=").append(txTime);
        // 生成播流地址
        StringBuffer sb1 = new StringBuffer();
        sb1.append(TencentYunVedioUtil.getPlayUrlHlsM(streamName))
                .append("?").append("txSecret=")
                .append(TencentYunVedioUtil.pushSign(TencentYunVedioUtil.play_key, streamName, txTime))
                .append("&").append("txTime=").append(txTime);
        HashMap<Object, Object> map = new HashMap<>();
        // 推流地址
        map.put("streamKey", sb);
        // 播流地址
        map.put("playUrl", sb1);
        // 服务器地址
        map.put("pushUrlHead", TencentYunVedioUtil.pushUrl);
        return map;
    }
    /**
    * @Author MJL
    * @Description todo 生成拉流路径地址
     *
     * playUrlHead  拉流的头地址
     * stream_id  直播码  调用该方法genLiveCode() 生成
     * play_key  拉流鉴权
     * end 如 .flv结尾
     *
    * @Date 2023/8/23 10:32
    * @Return  * @param null
    */
    @PostMapping("/createPlayUrl")
    public static String createPlayUrl(String streamName) {
        // 过期时间16进制的unix时间戳
        String txTime = TencentYunVedioUtil.to16Hex(TencentYunVedioUtil.addOneDay());
        StringBuffer sb = new StringBuffer();
        sb.append(TencentYunVedioUtil.getPlayUrlHlsM(streamName))
                .append("?").append("txSecret=")
                .append(TencentYunVedioUtil.pushSign(TencentYunVedioUtil.play_key, streamName, txTime))
                .append("&").append("txTime=").append(txTime);
        return sb.toString();
    }
}
